<template>
	<view>
		<u-popup :show="showLoginPop" mode="bottom" :round="10" @close="close">
			<mploginpopup></mploginpopup>
		</u-popup>
		
		<button class="submit-btn" hover-class="other-button-hover" @click="mnpLogin">
			授权登录
		</button>
	</view>
</template>

<script>
	import mploginpopup from '@/components/mplogin-popup/mplogin-popup.vue';
	import {
		getCode
	} from "@/utils/login";
	export default {
		components: {
			mploginpopup
		},
		data() {
			return {
				showLoginPop: false,
				avatar: "",
				nickname: "",
			}
		},
		methods: {
			// 关闭
			close(){
				this.showLoginPop = false;
			},
			// 小程序微信登录
			async mnpLogin() {
				uni.showLoading({
					title: "登录中...",
					mask: true,
				});
				const wxCode = await getCode();
				// 这里假设通过code注册成功,然后进一步判断
				let res = await this.$api.mnpLogin({
					code: wxCode
				});
				if(res.code == 1){
					uni.hideLoading()
					uni.setStorageSync('token',res.data.token);
					if(res.data.is_new_user == 1){
						this.$nextTick(function(){
							this.showLoginPop = true;
						})
					}else{
						uni.showToast({
							title: '登录成功',
						});
						setTimeout(() => {
							uni.reLaunch({
								url: '/pages/home/home'
							})
						}, 1500)
					}
				}else{
					uni.hideLoading()
				}
			},
			// 关闭
			closePopup() {
				this.showLoginPop = false;
			}
		}
	}
</script>

<style scoped lang="scss">
	.submit-btn {
		height: 82rpx;
		line-height: 82rpx;
		border-radius: 41px;
		color: #fff;
		font-size: 30rpx;
		margin: 80rpx 35rpx 60rpx;
		background: #00A7FF;
	}
</style>
